package com.carleasoft.mps.cms.business.fileUpload.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.carleasoft.mps.cms.business.fileUpload.bean.entity.InformationLibrary;
import com.carleasoft.mps.cms.business.fileUpload.bean.entity.InformationTypeEntity;
import com.carleasoft.mps.cms.business.fileUpload.bean.po.InformationLibraryPo;
import com.carleasoft.mps.cms.business.fileUpload.bean.po.LabelVo;
import com.carleasoft.mps.cms.business.fileUpload.bean.vo.InformationLibraryVo;
import com.carleasoft.mps.cms.business.fileUpload.bean.vo.InformationTypeVo;
import com.carleasoft.mps.cms.business.fileUpload.service.InformationLibraryService;
import com.carleasoft.mps.cms.business.fileUpload.service.InformationTypeService;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * 文件上传
 *
 * @author yanghao
 * @date 2022-02-17 16:44
 * @project mps
 */
@RestController
@RequestMapping("/information")
public class InformationLibraryController {

    @Autowired
    private InformationLibraryService informationLibraryService;
    @Autowired
    private InformationTypeService informationTypeService;

    /**
     * 融办上传资料
     *
     * @param informationLibraryPo
     * @param request
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody InformationLibraryPo informationLibraryPo, HttpServletRequest request) {
//        IBeanUtil.paramVerify(informationLibraryPo, "accessoryName", "accessorySuffix", "accessorySize");
        informationLibraryPo.setTypeCode(informationLibraryPo.getInformationTypeCode());
        // 获取用户信息
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        Boolean isOk = informationLibraryService.add(informationLibraryPo, user);
        return R.ok(isOk);
    }

    /**
     * 编辑
     *
     * @param informationLibraryPo
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody InformationLibraryPo informationLibraryPo) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        informationLibraryPo.setTypeCode(informationLibraryPo.getInformationTypeCode());
        Boolean isOk = informationLibraryService.updateInformation(informationLibraryPo, user);
        return R.ok(isOk);
    }

    /**
     * 分页查询资料库
     *
     * @param informationLibraryPo
     * @return
     */
    @PostMapping("/pageQuery")
    public R pageQuery(@RequestBody InformationLibraryPo informationLibraryPo) {

        Page page = informationLibraryService.pageQuery(new Page(informationLibraryPo.getCurrent(), informationLibraryPo.getSize()), informationLibraryPo);

        return R.ok(page);
    }

    /**
     * 查询详情
     *
     * @param informationLibraryPo
     * @return
     */
    @PostMapping("/selectById")
    public R selectById(@RequestBody InformationLibraryPo informationLibraryPo) {
        InformationLibrary library = informationLibraryService.selectById(informationLibraryPo);
        if (Objects.isNull(library)) {
//            throw new BusinessException(BusinessErrorEnum.DATABASE_NO);
        }
        return R.ok(library);
    }

    /**
     * 门户分页查询资料共享
     *
     * @param informationLibraryPo
     * @return
     */
    @PostMapping("/eipPage")
    public R eipPage(@RequestBody InformationLibraryPo informationLibraryPo) {
        Page<InformationLibraryVo> page = informationLibraryService.eipPage(new Page(informationLibraryPo.getCurrent(), informationLibraryPo.getSize()), informationLibraryPo);
        return R.ok(page);

    }


    /**
     * 根据id删除资料库
     *
     * @param
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody InformationLibraryPo informationLibraryPo) {
        // 获取用户信息
        UserCacheData userInfo = ThreadUserContext.getCurrentCacheUser();
        Boolean isOk = informationLibraryService.delete(informationLibraryPo.getAccessoryCode(), userInfo);
        return R.ok(isOk);
    }

    /**
     * 查询标签集合
     *
     * @return
     */
    @GetMapping("/labelList")
    public R labelList() {
        List<LabelVo> labelVos = informationLibraryService.labelList();
        return R.ok(labelVos);
    }

    /**
     * 置顶/取消
     *
     * @param informationLibraryPo
     * @return
     */
    @PostMapping("/isTop")
    public R isTop(@RequestBody InformationLibraryPo informationLibraryPo) {
        IBeanUtil.paramVerify(informationLibraryPo, "isTop", "id");
        boolean isOK = informationLibraryService.isTop(informationLibraryPo);
        return R.ok(isOK);
    }


    /**
     * 添加/修改  软件下载分类
     *
     * @param informationTypeEntity
     * @return
     */
    @PostMapping("/saveType")
    public R saveType(@RequestBody InformationTypeEntity informationTypeEntity) {
        IBeanUtil.paramVerify(informationTypeEntity, "informationTypeName");
        boolean isOk = informationTypeService.saveType(informationTypeEntity);
        return R.ok(isOk);
    }

    /**
     * 软件下载分类-树
     *
     * @param informationTypeEntity
     * @return
     */
    @PostMapping("/tree")
    public R tree(@RequestBody InformationTypeEntity informationTypeEntity) {
        List<InformationTypeVo> informationTypeVos = informationTypeService.tree(informationTypeEntity);
        return R.ok(informationTypeVos);
    }

    /**
     * 删除分类
     *
     * @param informationTypeEntity
     * @return
     */
    @PostMapping("/deleteType")
    public R delete(@RequestBody InformationTypeEntity informationTypeEntity) {
        IBeanUtil.paramVerify(informationTypeEntity, "informationTypeCode");
        boolean isOk = informationTypeService.delete(informationTypeEntity);
        return R.ok(isOk);
    }


}
